package com.itheima.task.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity //开启Security的自动配置
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启全局方法授权的开关
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 配置用户详情信息 ----> 用户名, 密码 , 权限 -----------> 明文密码
     * @return
     */
   /* @Override
    @Bean
    protected UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();

        //封装用户信息
        //SpringSecurity中角色标识规范 : ROLE_XXXX
        userDetailsManager.createUser(User.withUsername("itheima").password("{noop}123456").authorities("ROLE_ADMIN","P1").build());
        userDetailsManager.createUser(User.withUsername("itcast").password("{noop}123456").authorities("ROLE_ADMIN","P2","P3").build());

        return userDetailsManager;
    }*/


    /**
     * 配置用户详情信息 ----> 用户名, 密码 , 权限 ---------> 加密密码
     * @return
     */
    /*@Override
    @Bean
    protected UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();

        //封装用户信息
        //SpringSecurity中角色标识规范 : ROLE_XXXX
        userDetailsManager.createUser(User.withUsername("itheima").password("$2a$10$qcKkkvsoClF9tO8c9wlR/ebgU8VM39GP5ZUdsts.XSPDmE40l.BP2").authorities("ROLE_ADMIN","P1").build());
        userDetailsManager.createUser(User.withUsername("itcast").password("$2a$10$qcKkkvsoClF9tO8c9wlR/ebgU8VM39GP5ZUdsts.XSPDmE40l.BP2").authorities("ROLE_ADMIN","P2","P3").build());

        return userDetailsManager;
    }*/

    //密码加密方式
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()//开启表单登录
            .and()
            .logout()  //开启退出功能
            .and()
            .csrf().disable() //关闭CSRF(跨站请求伪造)策略

            .authorizeRequests()
            .antMatchers("/register").permitAll()    //不登录即可访问
//            .antMatchers("/hello").hasRole("ADMIN") //指定具有ROLE_ADMIN角色才可以访问
//            .antMatchers("/say").hasAuthority("P1") //指定具有P1权限擦可以访问
            .anyRequest().authenticated(); //其他请求认证即可访问
    }
}
